<template>
  <div>
    <el-drawer ref="userDetailFrom" :visible.sync="dialogUserDetail" size="1200px" @close="handleClose">
      <div slot="title" class="demo-drawer_title"></div>
      <div class="demo-drawer__content">
        <div class="description" v-if="userDetailData">
          <div class="con-head">
            <img :src="userDetailData.avatar" alt="" />
            <span class="nickname">{{ userDetailData.nickname }}</span>
          </div>
          <div class="acea-row info-row">
            <div class="info-row-item">
              <div class="info-row-item-title">余额</div>
              <div>{{ userDetailData.nowMoney }}</div>
            </div>
            <div class="info-row-item">
              <div class="info-row-item-title">用户经验</div>
              <div>{{ userDetailData.experience }}</div>
            </div>
            <div class="info-row-item">
              <div class="info-row-item-title">等级</div>
              <div>{{ userDetailData.grade }}</div>
            </div>
            <div class="info-row-item">
              <div class="info-row-item-title">佣金金额</div>
              <div>{{ userDetailData.brokeragePrice }}</div>
            </div>
            <div class="info-row-item">
              <div class="info-row-item-title">用户积分</div>
              <div>{{ userDetailData.integral }}</div>
            </div>
            <div class="info-row-item">
              <div class="info-row-item-title">用户购买次数</div>
              <div>{{ userDetailData.payCount }}</div>
            </div>
            <div class="info-row-item">
              <div class="info-row-item-title">连续签到天数</div>
              <div>{{ userDetailData.signNum }}</div>
            </div>
          </div>
          <div class="user-info">
            <div class="section">
              <div class="section-hd">基本信息</div>
              <div class="section-bd">
                <div class="item">
                  <div>用户电话：</div>
                  <div class="value">{{ userDetailData.phone }}</div>
                </div>
                <div class="item">
                  <div>真实姓名：</div>
                  <div class="value">{{ userDetailData.realName || '-' }}</div>
                </div>
                <div class="item">
                  <div>用户账号：</div>
                  <div class="value">{{ userDetailData.account || '-' }}</div>
                </div>
                <div class="item">
                  <div>生日：</div>
                  <div class="value">{{ userDetailData.birthday || '-' }}</div>
                </div>
                <div class="item">
                  <div>性别：</div>
                  <div class="value">
                    {{
                      userDetailData.sex == 0
                        ? '未知'
                        : userDetailData.sex == 1
                        ? '男'
                        : userDetailData.sex == 2
                        ? '女'
                        : '保密'
                    }}
                  </div>
                </div>
                <div class="item">
                  <div>国家：</div>
                  <div class="value">{{ userDetailData.country == 'CN' ? '中国' : '其他' || '-' }}</div>
                </div>
                <div class="item">
                  <div>用户地址：</div>
                  <div class="value">
                    {{
                      userDetailData.province +
                        userDetailData.city +
                        userDetailData.district +
                        userDetailData.address || '-'
                    }}
                  </div>
                </div>
              </div>
            </div>
          </div>
        
          <div class="user-info">
            <div class="section">
              <div class="section-hd-other">病例记录</div>
             <div class="section-bd">
              <!-- 添加一个包裹元素 -->
            <div class="table-pagination-wrapper">
                <!-- 新增病例列表展示 -->
                <el-table :data="userCaseListData.list" style="width: 200%">
                  <el-table-column prop="createTime" label="时间" />
                  <el-table-column prop="checkItem" label="检查项目" />
                  <!-- 可以根据实际返回数据添加更多列 -->
                  <el-table-column label="操作">
                    <template slot-scope="scope">
                      <el-button size="small" @click="viewCase(scope.row.id)">查看</el-button>
                      <el-button size="small" type="primary" @click="editCase(scope.row.id)">编辑</el-button>

                      <el-button size="small" type="danger" @click="deleteCase(scope.row.id)">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>

                <!-- 添加分页器 -->
                  <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="tableFrom.limit"
                    :current-page="tableFrom.page"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="userCaseListData.total">
                  </el-pagination>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </el-drawer>


    <el-dialog
      title="病例详情"
      :visible.sync="caseDetailDialogVisible"
      width="70%"
      :before-close="handleCloseDialog">
      <el-form :inline="true" :model="currentCaseDetail" class="responsive-form">
        <el-form-item label="检查项目">
          <!-- <el-input v-model="currentCaseDetail.checkItem" disabled></el-input> -->

          <el-select :disabled="isDisabled" v-model="currentCaseDetail.checkItem" placeholder="请选择" class="selWidth">
            <el-option v-for="item in checkItemList" :key="item.value" :label="item.name" :value="item.value" />
          </el-select>

        </el-form-item>
       
         <!-- 添加检查结果输入框 -->
        <el-form-item label="检查结果">
          <Tinymce :disabled="!isDisabled"  v-model.trim="currentCaseDetail.content"></Tinymce>
        </el-form-item>

         <!-- 添加检查医生输入框 -->
        <el-form-item label="检查医生" class="check-doctor-item">
          <el-input v-model="currentCaseDetail.checkDoctor" disabled></el-input>
        </el-form-item>

       

      </el-form>

       <div slot="footer" v-if="!isDisabled" class="custom-dialog-footer">
          <el-button @click="handleCloseDialog()">取消</el-button>
          <el-button type="primary" @click="handlerSubmit()">确定</el-button>
        </div>
            
    </el-dialog>

  </div>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import { userDetailApi } from '@/api/user';
import { userCaseListApi,userCaseDetailApi,userCaseDeleteApi ,updateCase} from '@/api/case';
import Tinymce from '@/components/Tinymce/indexOfCase';

export default {
  name: 'detailUser',
  components: { Tinymce },
  props: {
    userNo: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      dialogUserDetail: false,
      userDetailData: {},
      userCaseListData: {},
      caseDetailDialogVisible: false, // 控制病例详情对话框显示隐藏
      currentCaseDetail: {
        uid:'',
        content: '',
        checkItem:'',
        checkDoctor:''
      }, // 当前查看的病例详情

      // 分页
      tableFrom: {
        page: 1,
        limit: 5
      },
     
      // 用户的uid
      uid:'',

      // 检查项目
      checkItemList: [
      // { value: 'item1', label: '项目一' },
      // { value: 'item2', label: '项目二' },
      // { value: 'item3', label: '项目三' }
      ],

      // 查看编辑
      isDisabled: false 
    };
  },
  methods: {
    handleCloseDialog() {
      this.caseDetailDialogVisible = false;
    },
    handleClose() {
      this.dialogUserDetail = false;
    },
    getUserDetail(id) {
      userDetailApi(id).then((res) => {
        this.userDetailData = res;
      });
      this.caseList(id);


    },
    // 病例列表
    caseList(uid){
      this.uid = uid;
      this.tableFrom.uid = uid;
      userCaseListApi(this.tableFrom).then((res) => {
        console.log('打印用户病例',res);
        this.userCaseListData = res;
      });
  
    },

    //页数更改
    handleCurrentChange(val) {
      this.tableFrom.page = val;
      this.caseList(this.uid);
    },

    pageChange(page) {
      this.tableFrom.page = page;
      this.caseList(this.uid);
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.caseList(this.uid);
    },

    // 查看
    viewCase(id) {
      this.isDisabled = true;
      this.caseDetailDialogVisible = true;

      userCaseDetailApi(id).then((res) => {
        console.log('详情：',res);
        this.currentCaseDetail = res;
      });
    },
    // 编辑
    editCase(id) {
      console.log('编辑病例:');

      this.isDisabled = false;
      this.caseDetailDialogVisible = true;

      userCaseDetailApi(id).then((res) => {
        this.currentCaseDetail = res;
      });

    },
    // 删除
    deleteCase(id) {
      console.log('删除病例:', id);
      
      userCaseDeleteApi(id).then((res) => {
        this.$message.success('删除成功');
        this.caseList(this.uid);
          // 删除之后，刷新列表
      });
    },

    // 提交
    handlerSubmit() {
      this.caseDetailDialogVisible = false;
      // 点击了确定
      console.log('打印内容：',this.currentCaseDetail);
      // 调接口

      updateCase(this.currentCaseDetail)
        .then(async (res) => {
          this.$message.success('编辑成功');
          this.caseList(this.uid);
        })
        .catch((res) => {
        });

    },


  },
};
</script>

<style scoped lang="scss">
::v-deep .el-drawer__header {
  display: flex !important;
  align-items: flex-start !important;
  padding: 15px 15px 0 15px !important;
  margin: 0 !important;
}
::v-deep .el-drawer__body{
  padding: 0 0 30px 0 !important;
}
::v-deep .demo-drawer_title {
  width: 90%;
}
.InvoiceList {
  ::v-deep.el-collapse-item__header {
    font-size: 12px;
    color: #606266;
  }
}

.wrapper {
  background-color: #fff;
  margin-top: 7px;
  padding: 10px 12px;
  &-num {
    font-size: 10px;
    color: #999999;
  }

  &-title {
    color: #666666;
    font-size: 12px;
  }

  &-img {
    width: 60px;
    height: 60px;
    margin-right: 10px;
    border-radius: 7px;
    overflow: hidden;
    margin-bottom: 10px;

    image {
      width: 100%;
      height: 100%;
    }

    &:nth-child(5n) {
      margin-right: 0;
    }
  }
}

.title {
  font-size: 36px;
}

.demo-drawer__content {
  padding: 0 30px;
}

.demo-image__preview {
  display: inline-block;
  .el-image {
    width: 50px;
    height: 50px;
  }
}

.logistics {
  align-items: center;
  padding: 10px 0px;
  .logistics_img {
    width: 45px;
    height: 45px;
    margin-right: 12px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .logistics_cent {
    span {
      display: block;
      font-size: 12px;
    }
  }
}

.trees-coadd {
  width: 100%;
  height: 400px;
  border-radius: 4px;
  overflow: hidden;
  .scollhide {
    width: 100%;
    height: 100%;
    overflow: auto;
    margin-left: 18px;
    padding: 10px 0 10px 0;
    box-sizing: border-box;
    .content {
      font-size: 12px;
    }

    .time {
      font-size: 12px;
      color: #2d8cf0;
    }
  }
}

.title {
  margin-bottom: 14px;
  color: #303133;
  font-weight: 500;
  font-size: 14px;
}

.description {
  &-term {
    display: table-cell;
    padding-bottom: 5px;
    line-height: 20px;
    width: 50%;
    font-size: 12px;
    color: #606266;
  }
  ::v-deep .el-divider--horizontal {
    margin: 12px 0 !important;
  }
}
.description-term img {
  width: 60px;
  height: 60px;
}
.description-term {
  display: flex;
  align-items: center;
}
.con-head {
  display: flex;
  align-items: center;
  img {
    width: 60px;
    height: 60px;
    margin-right: 15px;
    border-radius: 50%;
  }
  .nickname {
    font-weight: 500;
    font-size: 16px;
    line-height: 16px;
    color: rgba(0, 0, 0, 0.85);
  }
}
::v-deep .el-drawer__header {
  height: 0 !important;
  margin: 0 !important;
  padding: 15px !important;
}

.info-row {
  flex-wrap: nowrap;
  padding: 20px 35px 24px 0;

  &-item {
    flex: none;
    width: 155px;
    font-size: 14px;
    line-height: 14px;
    color: rgba(0, 0, 0, 0.85);

    &-title {
      margin-bottom: 12px;
      font-size: 13px;
      line-height: 13px;
      color: #666666;
    }
  }
}
.user-info {
  .section {
    padding: 25px 0;
    border-top: 1px dashed #eeeeee;

    &-hd {
      padding-left: 10px;
      border-left: 3px solid var(--prev-color-primary);
      font-weight: 500;
      font-size: 14px;
      line-height: 16px;
      color: #303133;
    }

    &-bd {
      display: flex;
      flex-wrap: wrap;
    }

    .item {
      width: 30%;
      display: flex;
      margin: 16px 30px 0 0;
      font-size: 13px;
      color: #606266;

      &:nth-child(3n + 3) {
        margin: 16px 0 0;
      }
    }

    .value {
      flex: 1;
    }
    .avatar {
      width: 60px;
      height: 60px;
      overflow: hidden;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}
.section-hd-other {
  padding-left: 10px;
  border-left: 3px solid var(--prev-color-primary);
  font-weight: 500;
  font-size: 14px;
  line-height: 16px;
  color: #303133;
}

.responsive-form {
  display: flex;
  flex-direction: column; /* 让表单元素垂直排列 */
  align-items: flex-start; /* 左对齐元素 */
}

.responsive-form .el-form-item {
  width: 100%; /* 让每个表单项占满父容器宽度 */
  box-sizing: border-box; /* 确保 padding 和 border 包含在宽度内 */
}

.table-pagination-wrapper {
  display: flex;
  flex-direction: column;
  width: 100%;
  margin-top: 1%;
}
/* 添加自定义样式使按钮居中 */
.custom-dialog-footer {
  text-align: center; 
}
</style>
